package Servlets;

import main.ServerGlobals;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import protocol.HTTPRequest;
import protocol.HTTPResponse;
import protocol.Headers;
import SQL.SQLLiteHandler;

public class remove_user extends add_user {

	private boolean isError788(HTTPRequest httpRequest,
			ServerGlobals serverGlobals) {
		JSONArray rs = this.fSQLLiteHandler.Query("select "
				+ WhatsAppConstatnts.USERNAME
				+ " from "
				+ WhatsAppConstatnts.TABLE_GROUPS
				+ " where "
				+ WhatsAppConstatnts.GROUP_NAME
				+ "='"
				+ SQLLiteHandler.clean(httpRequest.getfHttpmsg().toJSONObject()
						.get("Target").toString())
				+ "' and "
				+ WhatsAppConstatnts.PHONE
				+ " ='"
				+ SQLLiteHandler.clean(httpRequest.getfHttpmsg().toJSONObject()
						.get("User").toString()) + "'");
		if (rs.size() == 0) {
			return true;
		}
		return false;
	}

	@Override
	public HTTPResponse process(HTTPRequest httpRequest,
			ServerGlobals serverGlobals) {
		if (!this.isCorrectMethod(httpRequest, Headers.POST)) {
			return new HTTPResponse(Headers.STATUS_405, Headers.HTTP_VERSION,
					new JSONObject(), "", false);
		}
		if (!this.isAuthorized(httpRequest, serverGlobals)) {
			return new HTTPResponse(Headers.STATUS_403, Headers.HTTP_VERSION,
					new JSONObject(), "", false);
		}
		if (!this.areParametersRequiredSent(new String[] { "Target", "User" },
				httpRequest)) {
			return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
					new JSONObject(), WhatsAppConstatnts.ERROR_336, false);
		}

		if (isError770(httpRequest, serverGlobals)) {
			return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
					new JSONObject(), WhatsAppConstatnts.ERROR_769, false);
		}

		if (isError669(httpRequest, serverGlobals)) {
			return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
					new JSONObject(), WhatsAppConstatnts.ERROR_668, false);
		}

		if (isError788(httpRequest, serverGlobals)) {
			return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
					new JSONObject(), WhatsAppConstatnts.ERROR_788, false);
		}

		try {
			JSONArray rs = this.fSQLLiteHandler.Query("delete from "
					+ WhatsAppConstatnts.TABLE_GROUPS
					+ " where "
					+ WhatsAppConstatnts.GROUP_NAME
					+ "='"
					+ SQLLiteHandler.clean(httpRequest.getfHttpmsg()
							.toJSONObject().get("Target").toString())
					+ "' and "
					+ WhatsAppConstatnts.PHONE
					+ " ='"
					+ SQLLiteHandler.clean(httpRequest.getfHttpmsg()
							.toJSONObject().get("User").toString()) + "'");
		} catch (RuntimeException e) {
			if (e.getMessage().equals(SQLLiteHandler.SQL_CONCURRENCY)) {
				return new HTTPResponse(Headers.STATUS_200,
						Headers.HTTP_VERSION, new JSONObject(),
						WhatsAppConstatnts.ERROR_788, false);
			}
		}
		return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
				new JSONObject(), httpRequest.getfHttpmsg().toJSONObject()
						.get("User").toString()
						+ " removed from "
						+ httpRequest.getfHttpmsg().toJSONObject()
								.get("Target").toString(), false);
	}

}
